<?php

/**
* Class : phpargv
*
* @author  :  Dennis T Kaplan
*             URL: http://linux.PzzAzz.net
*        
* @version :  1.0
* Date     :  June 17, 2009
* Purpose  :  Capture command line arguments in to your php script
*/ 

fileget('rstrstr.php');

class argv
    {
        public function run($argv)
            {
                $this->argv = $argv;
                $this->remove_self();
                $this->check_help();
                $this->args_array();
                $this->check_args();
                return $this->argv;
            }

        public function set_arg($arg)
            {
                $this->arg = $arg;
                $this->args[$this->arg]['required'] = FALSE;
            }
        
        public function set_required($i=FALSE)
            {
                $this->args[$this->arg]['required'] = $i;
            }

        public function set_value($v)
            {
                $this->args[$this->arg]['value'][] = $v;
            }

        public function set_text($z)
            {
                $this->text = $z;
            }
            
        public function args_view()
            {
                $r = "\n$this->text\n\nOptions:\n";
                foreach($this->args as $arg=>$v)
                    {
                        $s='';
                        $i='Optional';
                        if($v['required'] === TRUE)
                            {
                                $i = "Required";
                            }
                        foreach($v['value'] as $value)
                            {
                                $s .= "$value | ";
                            }
                        $s=trim($s,'| ');
                        $r.="\t-$arg=[ $s ] <$i>\n";
                    }
                $r .= "\n\nUsage:\n\t php $_SERVER[PHP_SELF] -arg=value\n\n";
                return $this->help = $r;
            }
            
        private function check_args()
            {
                if(is_array($this->argv))
                    {
                        foreach($this->argv as $k=>$v)
                            {
                                if (!array_key_exists($k, $this->args))
                                    {
                                        $this->create_help();
                                        exit;
                                    }
                            }
                    }else{
                        $this->create_help();
                        exit;                        
                    }
            }
            
        private function check_help()
            {
                if (in_array($this->argv[1], array('--help', '-help', '-h', '-?'))) {
                    $this->create_help();
                }
            }

         private function create_help()
            {
                $this->args_view();
                $h="\n   This is a command line PHP script.\n";
                $h.= $this->help;
                $h.="\n\n";
                echo $h;
            }

        private function remove_self()
            {
                if($this->argv[0] == $_SERVER[PHP_SELF])
                    {
                        unset($this->argv[0]);
                    }
            }
        
        function args_array()
            {
                $t = $this->argv;
                unset($this->argv);
                foreach($t as $v)
                    {
                        $this->argv[trim(rstrstr($v,'='),'-')] = trim(strstr($v,'='),'=');
                    }
                unset($t);
            }

    }
?>
